package com.example.javaApi;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Juc {
    public static void main(String[] args) {
        LockJuc lockJuc = new LockJuc();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->lockJuc.func());
        executorService.execute(()->lockJuc.func());

    }


    static class LockJuc{
      private Lock lock=new ReentrantLock();
      public void func(){
          lock.lock();
          try{
              for(int i=0;i<10;i++){
                  System.out.print(i+" ");
              }
          }finally {
              lock.unlock();//避免死锁
          }
      }
    }
}
